package com.gank.util;

import android.app.Activity;
import android.app.Dialog;

import com.gank.R;

import java.lang.ref.WeakReference;

/**
 * Created by EricLi.
 * on 2017/5/16 in 下午2:09
 * Email: EricLi1235@gmial.com
 */

public class SplashScreen {
    private static Dialog mSplashDialog;
    private static WeakReference<Activity> mActivity;

    public static void show(final Activity activity, final boolean fullScreen) {
        if (activity == null) {
            return ;
        }
        mActivity = new WeakReference<Activity>(activity);//包装成为弱引用
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mSplashDialog = new Dialog(activity, fullScreen ? R.style.Splash_Fullscreen : R.style.SplashScreen_SplashTheme);
                mSplashDialog.setContentView(R.layout.launch_screen);

                if (!mSplashDialog.isShowing()) {
                    mSplashDialog.show();
                }
            }
        });
    }

    public static void hide(Activity activity){
        if (activity == null) activity = mActivity.get();
        if (activity == null) return;
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mSplashDialog != null && mSplashDialog.isShowing()) {
                    mSplashDialog.dismiss();
                }
            }
        });

    }
}
